﻿Imports System.IO


Public Class Form1

#Region "Revision Notes"
    '*************************************
    'Author:    Ivan Perez
    'Date:      12/22/2009
    'Purpose:   NPE network mapping tool.
    '*************************************

    'v1.0       *- Initial release
    'v1.1       *- (2/10/2010) Added "reconnect at logon" for persistant settings
    'v1.2       *- (7/15/2010) Added Support for NPE Manufacturing backup server
    'v1.3       *- (11/17/2010) updated paths to correct proe drive letters.
#End Region
    Private Const VERSION As String = "v1.4"
    Public oDS As New DataSet
    Private blnConfigLoaded As Boolean = False
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = "Network Mapping Wizard " & VERSION
        Me.Icon = My.Resources.npe_mapper
        Call btnDefaults_Click(Nothing, Nothing)
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim rectForm As New Rectangle(New Point(0, 0), Me.ClientSize)

        Dim l As New Drawing2D.LinearGradientBrush(rectForm, _
                Color.FromArgb(255, 98, 126, 189), _
                Color.LightGray, _
                Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal)
        e.Graphics.FillRectangle(l, rectForm)
    End Sub

    Private Sub btnDefaults_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefaults.Click
        Try
            Me.txtPWD.Text = ""
            Me.txtUser.Text = ""


            'SAMPLE USAGE
            Dim sDrives() As String, iDrive As Integer
            sDrives = ListAllDrives()

            Dim oCBO As ComboBox
            Dim obj As Object
            For Each obj In Me.Controls
                If TypeOf obj Is ComboBox Then
                    oCBO = CType(obj, ComboBox)

                    For iDrive = 67 To 90
                        If Not sDrives.Contains(Chr(iDrive) & ":\") Then
                            oCBO.Items.Add(Chr(iDrive))
                        End If
                    Next
                End If
            Next

            'Me.cboData.Text = "P"
            'Me.cboSoftware.Text = "R"
            'Me.cboMISC.Text = "M"
            'Me.cboShared.Text = "S"
            'Me.cboProE.Text = "T"
            'Me.cboDrafts.Text = "V"
            'Me.cboDoc.Text = "U"
        Catch ex As Exception

        End Try
    End Sub
    Public Function ListAllDrives() As String()
        Dim arDrives() As String
        arDrives = Directory.GetLogicalDrives()
        Return arDrives
    End Function


    Private Sub btnMapDrives_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMapDrives.Click
        If Me.txtUser.Text = "" And Me.txtPWD.Text = "" Then
            MessageBox.Show("Enter username and password.", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End If
        Dim sNetIP As String = txtServerIP.Text
        If sNetIP = "" Then
            MessageBox.Show("Invalid server address.", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End If


        Try

            For Each dr As DataRow In oDS.Tables("data").Rows

            Next

            'If Me.chkDoc.Checked Then Call MapIT(cboDoc.Text, sNetIP & chkDoc.Text)
            'If Me.chkShared.Checked Then Call MapIT(cboShared.Text, sNetIP & chkShared.Text)
            'If Me.chkData.Checked Then Call MapIT(cboData.Text, sNetIP & chkData.Text)
            ''If Me.chkData2.Checked Then Call MapIT(cboData2.Text, sNetIP & chkData2.Text)
            'If Me.chkDrafts.Checked Then Call MapIT(cboDrafts.Text, sNetIP & chkDrafts.Text)
            'If Me.chkMISC.Checked Then Call MapIT(cboMISC.Text, sNetIP & chkMISC.Text)
            ''If Me.chkProE.Checked Then Call MapIT(cboProE.Text, sNetIPProENPE & chkProE.Text)
            'If Me.chkSoftware.Checked Then Call MapIT(cboDoc.Text, sNetIP & chkSoftware.Text)

            MsgBox("Drive(s) have been successfully mapped")
        Catch ex As Exception
            MessageBox.Show("Error mapping drive(s).", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    
    Private Sub MapIT(ByVal sDriveLetter As String, ByVal sUNC As String)
        Dim WshNetwork As Object
        WshNetwork = CreateObject("WScript.Network")
        Try
            Call WshNetwork.MapNetworkDrive(sDriveLetter & ":", sUNC, chkPersistent.Checked, Me.txtUser.Text, Me.txtPWD.Text)
            '/persistent: YES



        Catch ex As Exception

        End Try

        'MsgBox("Drives has been map successful")

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Private Sub chkProE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub EditConfigToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditConfigToolStripMenuItem.Click
        If Not My.Computer.FileSystem.FileExists("config.xml") Then
            oDS = New DataSet
            oDS = CreateXML()
            oDS.WriteXml("config.xml", XmlWriteMode.WriteSchema)
        End If
    End Sub

    Private Function CreateXML() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable("data")
        dt.Columns.Add("ENABLE", Type.GetType("System.Boolean"))
        dt.Columns.Add("SHARE", Type.GetType("System.String"))
        dt.Columns.Add("DRIVE", Type.GetType("System.String"))
        ds.Tables.Add(dt)

        Dim dtC As New DataTable("config")
        dtC.Columns.Add("SERVER", Type.GetType("System.String"))
        dtC.Columns.Add("USER", Type.GetType("System.String"))
        dtC.Columns.Add("PASSWORD", Type.GetType("System.String"))
        dtC.Columns.Add("REMEMBER", Type.GetType("System.Boolean"))
        ds.Tables.Add(dtC)


        Return ds
    End Function

    Private Sub LoadConfigToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadConfigToolStripMenuItem.Click
        Try
            oDS = New DataSet
            oDS.ReadXml("config.xml", XmlReadMode.Auto)

            If oDS.Tables("config").Rows.Count > 0 Then
                txtServerIP.Text = Str(oDS.Tables("config").Rows(0)("SERVER"))
                txtUser.Text = Str(oDS.Tables("config").Rows(0)("USER"))
                txtPWD.Text = Str(oDS.Tables("config").Rows(0)("PASSWORD"))
                chkPersistent.Checked = IIf(oDS.Tables("config").Rows(0)("REMEMBER") = "1", True, False)
            End If

            blnConfigLoaded = True
        Catch ex As Exception
            com.PublishError(ex)
        End Try
    End Sub
End Class
